// 自动导入ui库
const antdList = [
	'Affix',
	'Alert',
	'Anchor',
	'App',
	'AutoComplete',
	'Avatar',
	'BackTop',
	'Badge',
	'Breadcrumb',
	'Button',
	'Calendar',
	'Card',
	'Carousel',
	'Cascader',
	'Checkbox',
	'Col',
	'Collapse',
	'ColorPicker',
	'ConfigProvider',
	'DatePicker',
	'Descriptions',
	'Divider',
	'Drawer',
	'Dropdown',
	'Empty',
	'Flex',
	'FloatButton',
	'Form',
	'Grid',
	'Image',
	'Input',
	'InputNumber',
	'Layout',
	'List',
	'Mentions',
	'Menu',
	'Modal',
	'Pagination',
	'Popconfirm',
	'Popover',
	'Progress',
	'QRCode',
	'Radio',
	'Rate',
	'Result',
	'Row',
	'Segmented',
	'Select',
	'Skeleton',
	'Slider',
	'Space',
	'Spin',
	'Splitter',
	'Statistic',
	'Steps',
	'Switch',
	'Table',
	'Tabs',
	'Tag',
	'TimePicker',
	'Timeline',
	'Tooltip',
	'Tour',
	'Transfer',
	'Tree',
	'TreeSelect',
	'Typography',
	'Upload',
	'Watermark',
	'message',
	'notification',
	'theme',
	'unstableSetRender'
];

// 成功提示
export const msgSuccess = function (content) {
	message.open({
		content,
		type: 'success'
	});
};

// 失败提示
export const msgError = function (content) {
	message.open({
		content,
		type: 'error'
	});
};

// 警告提示
export const msgWarning = function (content) {
	message.open({
		content,
		type: 'warning'
	});
};

// 信息提示
export const msgInfo = function (content) {
	message.open({
		content,
		type: 'info'
	});
};

// 提交确认
export const confirm = function (data) {
	let options = {};
	if (typeof data === 'string') {
		options.content = data;
	} else {
		options = { ...data };
	}

	const { content = '是否确认导出所有数据项？', title = '提示', okText = '确定', cancelText = '取消' } = options;
	return new Promise((resolve, reject) => {
		Modal.confirm({
			content,
			title,
			okText,
			cancelText
		})
			.then((res) => {
				resolve(res);
			})
			.catch((err) => {
				reject(err);
			});
	});
};

// 使用useEffect来模拟下class的componentDidMount和componentWillUnmount的功能。
import { useEffect } from 'react';
// 组件渲染完成
export const useMount = function (fn) {
	useEffect(() => {
		fn();
	}, []);
};
// 组件销毁
export const useUnmount = function (fn) {
	const ref = useRef(fn);
	ref.current = fn;
	useEffect(() => {
		return ref.current();
	}, []);
};
// 组件强制更新
export const useUpdate = function () {
	const [, setState] = useState({});
	return useCallback(() => setState({}), []);
};

/**
 * vite动态加载图片静态文件
 * @param {string} path 文件路径
 * @returns
 */

export const getImgUrl = function (path) {
	const modules = import.meta.glob('../assets/img/**/*', { eager: true });
	if (path && modules) {
		if (path.includes('../assets/img/')) {
			return modules[path].default;
		} else {
			return modules[`../assets/img/${path}`].default;
		}
	}
};

/**
 * 时间组件格式调整
 * @param {string} picker 时间选择器类型
 * @param {Boolean} Time 是否含有time
 * @returns 时间格式
 */
export const formatFn = function (picker, Time) {
	let format = 'YYYY-MM-DD';
	switch (picker) {
		case 'week':
			format = 'YYYY-MM-DD';
			break;
		case 'month':
			format = 'YYYY-MM';
			break;
		case 'quarter':
			format = 'YYYY-MM-DD';
			break;
		case 'year':
			format = 'YYYY';
			break;
	}
	if (Time) format = 'YYYY-MM-DD HH:mm:ss';

	return format;
};

/**
 * 模拟v-show
 * @param {Boolean} val 是否显示
 * @returns style样式
 */
export const vShow = function (val) {
	return {
		style: {
			display: val ? 'block' : 'none'
		}
	};
};

export const autoImport = {
	'@/utils/util': ['selectDictLabel', 'selectDictLabels', 'addDateRange'],
	'@/utils/autoImport': [
		'msgSuccess',
		'msgError',
		'msgInfo',
		'msgWarning',
		'confirm',
		'useMount',
		'useUnmount',
		'useUpdate',
		'getImgUrl',
		'formatFn',
		'vShow'
	],
	'react-redux': ['useSelector', 'useDispatch'],
	'prop-types': [['default', 'propTypes']],
	'@/components': ['MyCascader', 'MyDatePicker', 'MyForm', 'MyFormTable', 'MyIcon', 'MyInput', 'MyInputNumber', 'MyPermissions', 'MySelect'],
	antd: antdList,
	ahooks: ['useReactive', 'useAntdTable'],
	lodash: [
		'clone',
		'cloneDeep',
		'merge',
		'isEqual',
		'isArray',
		'uniqBy',
		'isDate',
		'isElement',
		'isFunction',
		'isString',
		'isObject',
		'throttle',
		'debounce',
		'isNumber',
		'omit',
		'shuffle',
		'camelCase',
		'upperFirst'
	]
};
